package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;

import com.douwong.student.R;
import com.douwong.student.adapter.PrepareResourceAdapter;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.db.PrepareReaourceDBService;
import com.douwong.student.model.PrepareResource;
import com.douwong.student.utils.FileUtils;
import com.douwong.student.view.PullToRefreshListView;

import java.util.ArrayList;

/**
 * 备课 已下载碎片
 * 
 * @author Administrator
 * 
 */
@SuppressLint("ValidFragment")
public class PrepareLoadedFragment extends Fragment {
	private View view;
	private LinearLayout prepare_ll, features_ll, job_ll, test_ll,
			courseware_ll, teaching_plan_ll, pic_ll, video_ll, other_ll;
	private int[] textView = new int[] { R.id.prepare_text, R.id.features_text,
			R.id.job_text, R.id.test_text, R.id.courseware_text,
			R.id.teaching_plan_text, R.id.pic_text, R.id.video_text,
			R.id.other_text };
	private PullToRefreshListView listview;
	private PrepareResourceAdapter adapter;
	private ArrayList<PrepareResource> list;
	private int titleSelect = 0;
	private boolean isResource = true;
	private ArrayList<PrepareResource>tempList=new ArrayList<PrepareResource>();
	private int i;
	private static final String mTAG = PrepareLoadedFragment.class
			.getSimpleName();

	public PrepareLoadedFragment(){}
	
	public PrepareLoadedFragment(int i) {
		this.i = i;
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(mTAG,"onResume...................");
	}


	public void updateDisplay(){

		if(BaseApplication.sUserBean!=null){
			updateData(0);
		}
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(mTAG,"onCreateView...................");
		view = inflater.inflate(R.layout.prepare_loaded_layout, null);
		initView();
		list = new ArrayList<PrepareResource>();
		adapter = new PrepareResourceAdapter(getActivity(), i, tempList);
		listview.setAdapter(adapter);
		if(BaseApplication.sUserBean!=null){
			updateData(0);
		}
		return view;
	}





	private void initView() {
		prepare_ll = (LinearLayout) view.findViewById(R.id.prepare_ll);
		features_ll = (LinearLayout) view.findViewById(R.id.features_ll);
		job_ll = (LinearLayout) view.findViewById(R.id.job_ll);
		test_ll = (LinearLayout) view.findViewById(R.id.test_ll);
		courseware_ll = (LinearLayout) view.findViewById(R.id.courseware_ll);
		teaching_plan_ll = (LinearLayout) view
				.findViewById(R.id.teaching_plan_ll);
		pic_ll = (LinearLayout) view.findViewById(R.id.pic_ll);
		video_ll = (LinearLayout) view.findViewById(R.id.video_ll);
		other_ll = (LinearLayout) view.findViewById(R.id.other_ll);

		listview = (PullToRefreshListView) view
				.findViewById(R.id.prepare_resource_listview);
		listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
		LoadOnClick onClick = new LoadOnClick();
		prepare_ll.setOnClickListener(onClick);
		features_ll.setOnClickListener(onClick);
		job_ll.setOnClickListener(onClick);
		test_ll.setOnClickListener(onClick);
		courseware_ll.setOnClickListener(onClick);
		teaching_plan_ll.setOnClickListener(onClick);
		pic_ll.setOnClickListener(onClick);
		video_ll.setOnClickListener(onClick);
		other_ll.setOnClickListener(onClick);
		showTextView(textView[0]);
		listview.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				if (isResource) {
					if(titleSelect==0){
						updateData(titleSelect);
						listview.onRefreshComplete();
					}else {
						getResourceList(titleSelect);
						listview.onRefreshComplete();
						adapter.notifyDataSetChanged();
					}
					
				} else {
					// updateIndroData(RequestAction.REFRESH, 1);
				}


			}


		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (isResource) {
					FileUtils
							.openFile(tempList.get(position-1).getFilePath(),
									tempList.get(position-1).getFileName(),
									getActivity());
				}
			}
		});
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
				dialog.setTitle("提示");
				dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				dialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						FileUtils.deleteFile(tempList.get(arg2-1).getFilePath());
						tempList.get(arg2-1).setLoaded(false);
						tempList.get(arg2-1).setLoading(false);
						tempList.get(arg2-1).setPause(false);
						PrepareReaourceDBService.getInstance(getActivity()).upDate(tempList.get(arg2-1));
						PrepareReaourceDBService.getInstance(getActivity()).deleteLoading(tempList.get(arg2-1).getId());
						Intent changeIntent = new Intent();
						changeIntent.setAction(CommonConfig.CHANGE_RESOURCE_STATE);
						changeIntent.putExtra("loadState", "Delete");
						changeIntent.putExtra("fileId", list.get(arg2-1).getId());
						getActivity().sendBroadcast(changeIntent);
						tempList.remove(arg2-1);
						adapter.notifyDataSetChanged();
					}
				});	
				dialog.show();
				return true;
			}
		});

	}

	private void updateData(int resourceType) {
		tempList.clear();
		list.clear();
		if(BaseApplication.sUserBean!=null){
			list.addAll(PrepareReaourceDBService.getInstance(getActivity())
					.queryLoaded(resourceType));
			//Collections.sort(list);
		}
		tempList.addAll(list);
		adapter.notifyDataSetChanged();
	}

	private class LoadOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			// 全部
			case R.id.prepare_ll:
				isResource = true;
				titleSelect = 0;
				showTextView(textView[0]);
//				listview.setAdapter(adapter);
				updateData(titleSelect);
				break;
			// 导读
			case R.id.features_ll:
				isResource = false;
				showTextView(textView[1]);
				break;
			// 任务
			case R.id.job_ll:
				isResource = false;
				showTextView(textView[2]);
				break;
			// 试卷
			case R.id.test_ll:
				isResource = false;
				showTextView(textView[3]);
				break;
			// 课件
			case R.id.courseware_ll:
				isResource = true;
				titleSelect = 1;
				showTextView(textView[4]);
//				listview.setAdapter(adapter);
				getResourceList(titleSelect);
				break;
			// 教案
			case R.id.teaching_plan_ll:
				isResource = true;
				titleSelect = 2;
				showTextView(textView[5]);
				listview.setAdapter(adapter);
				updateData(titleSelect);
				break;
			// 图片
			case R.id.pic_ll:
				isResource = true;
				titleSelect = 2;
				showTextView(textView[6]);
//				listview.setAdapter(adapter);
				getResourceList(titleSelect);
//				updateData(titleSelect);
				break;
			// 影音
			case R.id.video_ll:
				isResource = true;
				titleSelect = 3;
				showTextView(textView[7]);
				getResourceList(titleSelect);
//				updateData(titleSelect);
				break;
			// 其他
			case R.id.other_ll:
				showTextView(textView[8]);
				titleSelect=4;
				getResourceList(titleSelect);
//				updateData(titleSelect);
				break;
			default:
				break;
			}

		}

	}
	private void getResourceList(int type) {
		tempList.clear();
		list.clear();
		if(BaseApplication.sUserBean!=null){
			list.addAll(PrepareReaourceDBService.getInstance(getActivity())
					.queryLoaded(0));
		}
		
		for (int i = 0; i < list.size(); i++) {
			String suffix = list.get(i).getAttaSuffix();
			if (type == 2) {
				if (suffix.equals("jpg") || suffix.equals("png")) {
					tempList.add(list.get(i));
				}
			} else if (type == 1) {
				if (suffix.equals("ppt")) {
					tempList.add(list.get(i));
				}
			} else if (type == 3) {
				if (suffix.equals("mp4") || suffix.equals("avi") || suffix.equals("flv")) {
					tempList.add(list.get(i));
				}
			} else if (type == 4) {
				if (!suffix.equals("jpg") && !suffix.equals("png") && !suffix.equals("ppt") && !suffix.equals("mp4")
						&& !suffix.equals("avi") && !suffix.equals("flv")) {
					tempList.add(list.get(i));
				}

			}

		}
		adapter.notifyDataSetChanged();

	}
	private void showTextView(int id) {
		for (int i = 0; i < textView.length; i++) {
			if (id == textView[i]) {
				view.findViewById(id).setVisibility(View.VISIBLE);
			} else {
				view.findViewById(textView[i]).setVisibility(View.INVISIBLE);
			}

		}
	}

}
